package com.yx.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.common.vo.Result;
import com.yx.sys.entity.Post;
import com.yx.sys.entity.Reply;
import com.yx.sys.entity.Wiki;
import com.yx.sys.entity.WikiPostVO;
import com.yx.sys.service.IEditionService;
import com.yx.sys.service.IPostService;
import com.yx.sys.service.ITagsService;
import com.yx.sys.service.IWikiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 链接 前端控制器
 * </p>
 *
 * @author yx
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/wiki")
public class WikiController {
    @Autowired
    private IWikiService iWikiService;

    @Autowired
    private IEditionService editionService;

    @Autowired
    private ITagsService tagsService;

    @Autowired
    private IPostService postService;
//    @GetMapping("/all")
//    public Result<List<Wiki>> selectAllWiki(){
//        List<Wiki> list = iWikiService.getAllWiki();
//        return Result.success(list,"查询搜索wiki信息成功");
//    }
    @GetMapping("/wikiMes")
    public Result<?> selectWikiMes(@RequestParam(value = "eId",required = false) Integer eId,
                                   @RequestParam(value = "tId",required = false) Integer tId,
                                   @RequestParam(value = "status",required = false) Integer status
                                   ){
        List<?> wikiMes = iWikiService.getWikiMes(eId, tId,status);
        return Result.success(wikiMes,"获取节点信息成功");
    }
    //    根据wId修改节点状态
    @PutMapping("/delWiki")
    public Result<?> delComment(@RequestParam("wId") Integer wId,
                                @RequestParam("num") Integer num){
        System.out.println("wId:"+wId);
        LambdaUpdateWrapper<Wiki> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Wiki::getwId,wId);
        Wiki wiki = iWikiService.getOne(wrapper);
        System.out.println(wiki);
        boolean update = true;
        if(wiki!=null){
            wrapper.set(Wiki::getApproved,num);
            update = iWikiService.update(wrapper);
        }
        return Result.success(update,"删除违规节点成功");
    }
    @GetMapping("/list")
    public  Result<Map<String,Object>> getWikiList(
                                                    @RequestParam(value = "eId",required = false) Integer eId,
                                                    @RequestParam(value = "tId",required = false) Integer tId,
                                                    @RequestParam(value = "pageNo") Integer pageNo,
                                                    @RequestParam(value = "pageSize") Integer pageSize){
        Integer startId = pageSize*(pageNo-1);
        List<Wiki> postWikiMes = iWikiService.findPostWikiMes(eId, tId, startId, pageSize);
        Map<String,Object> data = new HashMap<>();
        if(postWikiMes.size()>0){
            data.put("selectTotal",iWikiService.list().toArray().length);
        }else{
            data.put("selectTotal",postWikiMes.size());
        }
        data.put("total",iWikiService.list().toArray().length);
        data.put("rows",postWikiMes);
        System.out.println(data);
        return Result.success(data,"查询搜索wiki信息成功");
    }
    @GetMapping("/{tId}")
    public Result<List<Wiki>> getWikiBytId(@PathVariable("tId") Integer tId){
        LambdaQueryWrapper<Wiki> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Wiki::gettId,tId).eq(Wiki::getApproved,1);
        List<Wiki> list = iWikiService.list(wrapper);
        return Result.success(list,"根据id查询搜索wiki信息成功");
    }

    @GetMapping("/wId")
    public Result<?> getWikiByWid(Integer wId){
        Map<String, Object> data = iWikiService.getWikiByWid(wId);
        return Result.success(data);
    }
}
